"""
plutus project - stock market crawler
https://code.google.com/p/plutus-crawler/
~~~~~~~~~~~~~~~~~~~~~~~~~~~

This module implements the methods to extract the information
present on the BM&F Bovespa website for stock information: 
http://www.bmfbovespa.com.br

~~~~~~~~~~~~~~~~~~~~~~~~~~~

"""

#!/usr/bin/python
#coding: utf-8

import sys
import urllib
import xml.etree.ElementTree as et

class plutus:
	
	class stock:
		
		def __init__(self):
			self.info = plutus.StockInfo()		
		
		def getByCode(self, cod = ""):
			params = {"codsocemi": cod}
			query = urllib.urlencode(params)
			url = "http://www.bmfbovespa.com.br/cotacoes2000/formCotacoesMobile.asp?%s" % query
			f = urllib.urlopen(url)
			contents = f.read()
			f.close()
			
			dom = et.fromstring(contents)
			
			if dom.tag == "ERROS":
				return False;
			
			self.info.cod = dom[0].attrib["CODIGO"]
			self.info.desc = dom[0].attrib["DESCRICAO"]
			self.info.value = dom[0].attrib["VALOR_ULTIMO"]
			self.info.osc = dom[0].attrib["OSCILACAO"]
			self.info.delay = dom[0].attrib["DELAY"]
			self.info.source = "BM&F Bovespa"
			
			return
			
	class StockInfo:
		
		def __init__(self):
			self.cod = ""
			self.desc = ""
			self.value = 0.0
			self.osc = 0.0
			self.delay = 0
			self.source = ""
			return
